package com.jxtti.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jxtti.entity.SubjectAuth;
import com.jxtti.model.District;
import com.jxtti.service.DistrictService;
import com.jxtti.service.SubjectAuthService;
import com.jxtti.utils.ExceptionUtil;
/**
 * 
 * <br>
 * <b>功能：</b>SubjectAuthController<br>
 * <b>作者：lwb</b><br>
 * <b>日期：</b> Nov 26, 2015 <br>
 * <b>版权所有：<b>版权所有(C) 2015，<br>
 */ 
@Controller
@RequestMapping("/subjectAuth") 
public class SubjectAuthController {
	
	private final static Logger log= Logger.getLogger(SubjectAuthController.class);
	
	// Servrice start
	@Autowired
	private SubjectAuthService  subjectAuthService; 
	
	
	@Autowired
	private DistrictService districtService;
	
	
	
	@RequestMapping(value="/listAuth") 
	@ResponseBody
	public ModelAndView  listAuth(@RequestParam Map<String, Object> data,  SubjectAuth subjectAuth, HttpServletRequest request,
			HttpServletResponse response) throws Exception{		
		ModelAndView mav = new ModelAndView();
		try {
			List<District> list= districtService.queryByListAuth(data);
			if(data.containsKey("org_id"))
			{
				String org_id = data.get("org_id").toString();
				for(District d : list)
				{
					if(org_id.contains(d.getId().toString()))
					{
						d.setIsCheck(1);
					}
				}
			}
			
			data.put("list", list);
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/subject/org_list.jsp");
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		return mav;
	}
	
	
	/**
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/queryByAuthId") 
	@ResponseBody
	public  Map<String,Object>   queryByAuthId(@RequestParam Map<String, Object> data,  HttpServletRequest request,
			HttpServletResponse response) throws Exception{	
			String id = data.get("id").toString();
			String []ids = id.split(",");
			StringBuffer sb = new StringBuffer();
			for(int i = 0; i< ids.length; i++)
			{
				Map m = new HashMap();
				m.put("id", ids[i]);
				District d = districtService.queryByAuthId(m);
				sb.append(d.getName() + "  ");
			}
			data.put("key", sb.toString());
			
			return data;
			
			
	}
	
	/**
	 * 
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/list") 
	@ResponseBody
	public ModelAndView  list(@RequestParam Map<String, Object> data,  SubjectAuth subjectAuth, HttpServletRequest request,
			HttpServletResponse response) throws Exception{		
		ModelAndView mav = new ModelAndView();
		try {
			data =  subjectAuthService.list(data, subjectAuth);
			mav.addObject("data", data);
			mav.setViewName("jxtti/com.jxtti/subjectAuth");
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		return mav;
	}
	
	
	
	
	
	/**
	 * 点击添加跳转到增加界面
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addInterface")
	@ResponseBody
	public ModelAndView addInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			mav.addObject("data", data);
			mav.setViewName("/pages/subjectAuth/add.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	
	
	
	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public Map<String,Object> save(@RequestParam Map<String, Object> data,
			SubjectAuth  subjectAuth, HttpServletRequest request,
			HttpServletResponse response) throws Exception{

		ModelAndView mav = new ModelAndView();
		try {
			// 保存数据
			//User user = AuthUtil.getUserInfo(request);
			//disStandard.setOperPerson(user.getUserName());
			int exeCount = subjectAuthService.add(subjectAuth);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
	
	
	
	/**
	 * 点击修改跳转到增加界面
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateInterface")
	@ResponseBody
	public ModelAndView updateInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, SubjectAuth  subjectAuth,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			subjectAuth = subjectAuthService.querySubjectAuthById(subjectAuth);
		
			data.put("subjectAuth", subjectAuth);
			mav.addObject("data", data);
			mav.setViewName("/pages/subjectAuth/update.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	
	
	
	
	
	
	/**
	 * 修改信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(@RequestParam Map<String, Object> data,
			SubjectAuth  subjectAuth, HttpServletRequest request,
			HttpServletResponse response) {
		try {
		//	User user = AuthUtil.getUserInfo(request);
		//	subjectAuth.setOperPerson(user.getUserName());
			// 修改
			SubjectAuth subjectAuthTem=subjectAuthService.querySubjectAuthById(subjectAuth);

			int exeCount = subjectAuthService.updateBySelective( subjectAuth);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
	

/**
	 * 删除信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam Map<String, Object> data,
			SubjectAuth  subjectAuth, HttpServletRequest request,
			HttpServletResponse response) {
		try {
		//	User user = AuthUtil.getUserInfo(request);
		//	subjectAuth.setOperPerson(user.getUserName());
		
			 subjectAuth=subjectAuthService.querySubjectAuthById( subjectAuth);
			int exeCount = subjectAuthService.update(subjectAuth);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}

}
